/*
**    Copyright (C) 2003-2011 Institute for Systems Biology
**                            Seattle, Washington, USA.
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 2.1 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

package org.systemsbiology.tsv;

import java.io.Serializable;

/**
 * Basic implementer of {@link Field}. Hashable.
 *
 *  Allows for direct reference passage into and out of a {@link Field} object. Callers are responsible for maintaining
 *  the integrity of references passed into and out of this object, lest the contents be erroneously written into.
 *
 *  If immutability of the contained data structure is desired, consider maintaining this constraint in your
 *  implementation of generic T.
 * @author trobinso
 */
public class BasicField<T extends Comparable<T> & Serializable> implements Field<T> {
    private static final long serialVersionUID = -3984986128471618785L;

    T contents = null;

    /**
     * Default constructor for BasicField
     */
    public BasicField() {}

    /**
     * Constructor for BasicField, requiring contents to be immediately supplied
     * @param contents T immediate contents for this Field
     */

    public BasicField(T contents) {
        this.contents = contents;
    }

    /**
     * @inheritDoc
     */
    public void setContents(T contents) {
        assert(contents != null);

        this.contents = contents;
    }

    /**
     * @inheritDoc
     */
    public T getContents() {
        assert(contents != null);

        return contents;
    }

    public int compareTo(Field<T> f) {
        assert(this.contents != null);

        return this.contents.compareTo(f.getContents());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        BasicField that = (BasicField) o;

        if (contents != null ? !contents.equals(that.contents) : that.contents != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return contents != null ? contents.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "BasicField{\"" + contents.toString() + "\"}";
    }
}
